<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>收货地址</title>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/common.css"/>

    <style type="text/css">
        body {
            font-size: 14px;
            background: #f3f3f3;
            color:#666;
        }

        .aui-list-view {
            background: #f3f3f3;
        }

        .contain {
        }

        .menuRgiht {
            float: right;
            margin-right: 20px;
        }

        .aui-list-view i.aui-iconfont {
            color: #000;
        }

        .addressTop, .addressCenter {
            padding: 3px 0;
        }

        .addressCenter {
            padding-bottom: 10px;
        }

        .aui-radio {
            width: 18px;
            height: 18px;
            margin: 0;
        }

        .aui-radio:checked:before, .aui-radio.aui-checked:before {
            content: "";
        }

        .aui-radio:checked:after, .aui-radio.aui-checked:after {
            left: 0;
            top: -4px;
        }

        .aui-radio:checked, .aui-radio.aui-checked {
            background-color: #327afa;
            border: solid 1px #327afa;
        }

        .aui-list-view i.aui-iconfont {
            margin-right: -2px;
        }

        .addressBottom {
            padding: 8px 0;
            overflow: hidden;
        }

        .aui-border-b:after {
            right: -120%;
            left: -15px;
        }

        .aui-radio-name {
            height: 24px;
            line-height: 24px;
            margin: 0;
            margin-left: 5px;
        }

        .defaultAddress {
            color: #327afa;
            top:3px;position: relative;
        }

        .aui-list-view-cell {
            padding-bottom: 0;
            margin-bottom: 10px;
            background: #fff;
        }

        .edit {
            margin-right: 10px;
        }
    </style>
</head>
<body>
<div class="contain">
    <ul id="addressList-content">

    </ul>
</div>
<script id="addressList-template" type="text/x-dot-template">
    {{ for(var i in it) { }}
    <li class="aui-list-view-cell">
        <div class="addressTop">
            <span class="userName">{{=it[i].truename}}</span>
            <span class="userPhone aui-pull-right">{{=it[i].mobile}}</span>
        </div>
        <div class="addressCenter aui-border-b">
            <span>收货地址：</span>
            <span>{{=it[i].city}} {{=it[i].address}}</span>
        </div>
        <div class="addressBottom ">
            <!-- defaultAddress为默认地址样式 -->
                    <span class="status defaultAddress">
                        {{? it[i].checkbox == 1}}
                        <input class="aui-radio" type="radio" name="demo1" checked tapmode>
                        <div class="aui-radio-name">默认地址</div>
                        {{??}}
                        <input class="aui-radio" type="radio" name="demo1" data-itemid="{{=it[i].itemid}}" tapmode
                               onclick="setDefault(this)">
                        <div class="aui-radio-name">设为默认</div>
                        {{?}}
                    </span>
            <span class="dele aui-pull-right" onclick="addressDelete({{=it[i].itemid}})"> <i
                    class="aui-iconfont aui-icon-delete"></i>删除</span>&nbsp;
                <span class="edit aui-pull-right" tapmode onclick="addressEdit({{=it[i].itemid}})"> <i
                        class="aui-iconfont aui-icon-edit"></i>编辑</span>
        </div>
    </li>
    {{ } }}
</script>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript" src="../../script/myInfo.js"></script>
<script type="text/javascript" src="../../script/doT.min.js"></script>
<script type="text/javascript" src="../../script/echo.js"></script>
<script type="text/javascript" src="../../script/jquery-1.11.3.js"></script>
<script type="text/javascript">
    apiready = function () {
        api.parseTapmode();
        getStorageAll();
        addressList(user_name);
        //重新更新地址监听
        api.addEventListener({
            name: 'addressAdd'
        }, function (ret) {
            if (ret && ret.value) {
                addressList(user_name);
            }
        });
    }

    //获取用户地址列表
    function addressList(userName) {
        showLoading();
        var url = "User/addressList/userName/" + userName;
        data = {};
        ajaxRequest(url, 'get', data, function (ret, err) {
            if (ret.status == 1) {
                var content = $api.byId('addressList-content');
                var tpl = $api.byId('addressList-template').text;
                var tempFn = doT.template(tpl);
                $api.html(content, '');
                $api.html(content, tempFn(ret.msg));
            } else {
                api.toast({
                    msg: ret.msg,
                    duration: 2000,
                    location: 'bottom'
                })
            }
        });
        hideLoading();
    }

    //用户地址编辑
    function addressEdit(addressId) {
        api.openWin({
            name: 'addressEdit',
            url: 'addressEdit.html',
            pageParam: {
                addressId: addressId
            }
            //delay: 300
        });
    }

    //设置默认地址
    function setDefault(obj) {
        var itemid, checkbox;
        itemid = $(obj).attr('data-itemid');
        var url = "User/addressUpdateCheckbox/";
        var data = {
            itemid: itemid,
            username: user_name,
            checkbox: 1,
        };
        ajaxRequest(url, 'post', data, function (ret, err) {
            if (ret.status == 1) {
                api.sendEvent({
                    name: 'addressAdd',
                    extra: {
                        key: 'true'
                    }
                });
            } else {
                api.toast({
                    msg: '更新失败',
                    duration: 2000,
                    location: 'bottom'
                });
            }
        });

        if ($(obj).parent().hasClass("defaultAddress")) {
            // 单击的是默认地址
            return;
        } else {
            // 设置为默认
            $(".contain").find("input[type='radio']").each(function () {
                $(this).parent().removeClass("defaultAddress");
                $(this).parent().find(".aui-radio-name").text("设为默认");
            })
            $(obj).parent().addClass("defaultAddress");
            $(obj).parent().find(".aui-radio-name").text("默认地址");
        }
    }

    function addressDelete(addressId) {
        if (confirm('确定删除吗')) {
            showLoading();
            var url = "User/addressDelete/";
            var data = {
                addressId: addressId,
                userName: user_name,
            };
            ajaxRequest(url, 'post', data, function (ret, err) {
                api.sendEvent({
                    name: 'addressAdd',
                    extra: {
                        key: 'true'
                    }
                });
                if (ret.status == 1) {
                    api.toast({
                        msg: ret.msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                } else {
                    api.openFrame({
                        name: 'addressAdd',
                        url: 'addressAdd.html',
                    });
                }
            });
            hideLoading();
        }

    }
</script>
</html>